package com.penghaisoft.microwms.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * session操作类
 * Created by zxcq0 on 2018/2/6.
 */
@Slf4j
public class SessionUtil {

    public static final String SESSION_USER_INFO = "SESSION:USERINFO:";
    public static final String SESSION_CURRENT_FACTORY = "SESSION:FACTORY:";
    public static final String SESSION_FACTORY_LIST = "SESSION:FACTORYLIST:";

    public static final String SESSION_COOKIE = "sessionCookie";

    /**
     * 数据放入缓存
     * @param redisTemplate
     * @param key
     * @param value
     */
    public static void setSessionValue(StringRedisTemplate redisTemplate, String key, String value) {
        redisTemplate.opsForValue().set(key,value);
    }

    /**
     * 放入值时加入过期时间 单位秒
     * @param redisTemplate
     * @param key
     * @param value
     * @param expireSec
     */
    public static void setSessionValueExpire(StringRedisTemplate redisTemplate,String key, String value,Long expireSec) {
        redisTemplate.opsForValue().set(key,value,expireSec, TimeUnit.SECONDS);
    }

    /**
     * 数据移除缓存
     * @param redisTemplate
     * @param key
     */
    public static void removeSessionValue(StringRedisTemplate redisTemplate,String key) {
        redisTemplate.delete(key);
    }

    /**
     * 获取sessionVal
     * @param redisTemplate
     * @param key
     * @return
     */
    public static Object getSessionValue(StringRedisTemplate redisTemplate,String key) {
        return redisTemplate.opsForValue().get(key);
    }


    /**
     * 是否存在
     * @param redisTemplate
     * @param key
     * @return
     */
    public static boolean existSessionValue(StringRedisTemplate redisTemplate,String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 设置key的过期时间
     * @param redisTemplate
     * @param key
     * @param expireSec
     */
    public static void setSessionValueExpireSec(StringRedisTemplate redisTemplate,String key,Long expireSec) {
        redisTemplate.expire(key,expireSec,TimeUnit.SECONDS);
    }

    /**
     * 放入session
     */
    public static void setSessionCookie(HttpServletResponse response, HttpServletRequest request){
        Cookie ck = new Cookie(SESSION_COOKIE,getSessionId(request));
        ck.setPath("/");
        response.addCookie(ck);
    }

    /**
     * 获取cookie中的sesssion
     * 获取sessionId
     * @return
     */
    public static String getSessionId(HttpServletRequest request){
        String sessionId = "";
        Cookie[] cks = request.getCookies();
        if (cks!=null){
            for (Cookie ck:	cks) {
                if (ck.getName().equals(SESSION_COOKIE)){
                    sessionId = ck.getValue();
                }
            }
        }
        if ("".equals(sessionId)){
            log.info("Cookie中 sessionCookie 为空 重新生成");
            sessionId = request.getSession().getId();
        }
        log.info("当前登录人sessionCookie = " + sessionId);
        return sessionId;
    }
}
